<?php
/**
 * 订单表order_log
 * String id
 * String order_id【订单号】
 * String site【网站】
 * String flag【是否退货】
 * String remark【备注】
 * String total_amount【总额】
 * String ship_company【邮递方式】
 * String yunfei【运费】
 * String add_time
 * String statu【状态：0为为做单，1为已做单】
 */
class App_Model_OrderLog extends App_Model_Abstract
{
	public function init()
	{
		parent::init();
		$this->table = App_Db_Table::factory('OrderLog');
	}

	public function add($row)
	{
		return $this->table->insert($row);
	}

	public function delete($id)
	{
		$db = $this->table->getAdapter();
		$where = $db->quoteInto('id = ?', $id);
	
		return $this->table->delete($where);
	}

	public function getOne($id)
	{
		$row = $this->table->find($id);
		if ($row) {
			return $row->current();
		}
		return array();
	}
	
	public function update($row, $orderid, $site)
	{
		$db = $this->table->getAdapter();
		$where=array();
		$where[] = $db->quoteInto('order_id = ?', $orderid);
		$where[] = $db->quoteInto('site = ?', $site);
		$where=join(' AND ',$where);
	
		return $this->table->update($row, $where);
	}
	
	/**根据订单号，网站等获取订单*/
	public function getOrderLog($fieldArray)
	{
		$select = $this->table->select();
		if($fieldArray!=null){
			foreach ($fieldArray as $field => $value){
				$select->where("$field = ?",$value);
			}
		}
		return $this->table->fetchAll($select);
	}
	
	public function getStaOrderLog($site,$starttime,$endtime)
	{
		$select = $this->table->select();
		$select->where('flag = ?','sub');
		if(!empty($site)){
			$select->where('site = ?',$site);
		}
		if(!empty($starttime)){
			$select->where('add_time > ?',$starttime);
		}
		if(!empty($endtime)){
			$select->where('add_time < ?',$endtime);
		}
		$select->order('add_time desc');
        return $this->table->fetchAll($select);
	}
	
	public function listAll($orderid,$site,$statu,$starttime,$endtime,$page=1,$limit=20)
	{
		$select = $this->table->select();
		if(!empty($orderid)){
			$select->where('order_id = ?',$orderid);
		}
		if(!($statu=='')){
			$select->where('statu = ?',$statu);
		}
		if(!empty($site)){
			$select->where('site = ?',$site);
		}
		if(!empty($starttime)){
			$select->where('add_time > ?',$starttime);
		}
		if(!empty($endtime)){
			$select->where('add_time < ?',$endtime);
		}
		$offset = ($page - 1) * $limit;
		$select->limit($limit,$offset);
		
		$select->order('add_time desc');
        return $this->table->fetchAll($select);
	}
	
	public function getTotalNum($orderid,$site,$statu,$starttime,$endtime)
	{
		$where="where 1 ";
		
		if(!empty($orderid)){
			$where.=" and order_id='$orderid'";
		}
		if(!empty($statu)){
			$where.=" and statu='$statu'";
		}
		if(!empty($site)){
			$where.=" and site='$site'";
		}
		if(!empty($starttime)){
			$where.=" and add_time>='".$starttime."'";
		}
		if(!empty($endtime)){
			$where.=" and add_time<='".$endtime."'";
		}
		return $this->table->getAdapter()->fetchOne("select count(*) from order_log ".$where); 
	}
}